<?php
/**
 * Banner Abstraction
 * 
 * @author MF
 * @version 2.0
 */

class BannerList extends A_Content  {
	
	/**
	 * Возвращаяет запись со  списком баннеров по Id
	 *
	 * @param Integer $id
	 * @return SingleBannerList
	 */
	public function getById($id) {
		if (!isset($this->_cashe_obj[$id])) {
			$this->_cashe_obj[$id] = new SingleBannerList($this->_top,$this,$id);
		}
		
		return $this->_cashe_obj[$id];
	}
	
	/**
	 * Adds new BannerList content and returns pointer to it object
	 *
	 * @return SingleBannerList
	 */
	public function add() {
		$this->_top->_sql->exeDummy("INSERT INTO {$this->_top->_sql->tab_pref}banner_list(add_date) VALUES(NOW());");
		$id = $this->_top->_sql->last_insert_id();
		return $this->getById($id);
	}
	
}


class SingleBannerList extends A_SingleContent {
	
	/**
	 * List of additional fields in json format
	 *
	 * @var array Json
	 */
	public $config;
	
	private $_banner_list = array();
	private $_cashe_obj = array();
	
	public  function loader()
	{
		unset($this->config);
		
		$get_data = $this->_top->_sql->exe("SELECT * FROM {$this->_top->_sql->tab_pref}banner_list c WHERE banner_list_id={$this->_content_id}");
		if (count($get_data)==0)
			throw $this->_top->Utils->PushError("BannerList object Id: {$this->_content_id} not found", 'Системная ошибка. Обратитесь к разработчикам');
		else {
			$this->_data = $get_data[0];
			
			$this->_banner_list = array();
			$get_banner_list = $this->_top->_sql->exe("SELECT banner_id FROM {$this->_top->_sql->tab_pref}banner c WHERE banner_list_id={$this->_content_id}");
			for ($l=0;$l<count($get_banner_list);$l++) {
				array_push($this->_banner_list,$get_banner_list[$l][0]);
			}
		}
		return true;
	}
	
	public function __get($param) {
		if ($param=='config') {
			Zend_Json::$useBuiltinEncoderDecoder = true;
			return Zend_Json::decode($this->json_config);
		} elseif (isset($this->_data[$param])) {
			return $this->_data[$param];
		} else {
			throw $this->_top->Utils->PushError('Geting Undefinded SingleBannerList object property','Системная ошибка. Обратитесь к разработчикам');
		}
	}
	
	public function __set($param,$value) {
		if ($param=='add_date' || $param=='banner_list_id') {
			return false;
		} elseif (isset($this->_data[$param])) {
			$this->_data[$param] = $value;
			$ins_value = $this->_top->Utils->prepareValueForSQL($value);
			$this->_top->_sql->exeDummy("UPDATE {$this->_top->_sql->tab_pref}banner_list SET `$param`='$ins_value',last_mod=NOW() WHERE news_id={$this->_content_id}");
		} else {
			throw $this->_top->Utils->PushError('Seting Undefinded SingleBannerList object property','Системная ошибка. Обратитесь к разработчикам');
		}
	}
	
	/**
	 * Returns pointer to instance of SingleBanner class  
	 *
	 * @param Integer $id
	 * @return SingleBanner
	 */
	public function getById($id) {
		if (!isset($this->_cashe_obj[$id])) {
			$this->_cashe_obj[$id] = new SingleBanner($this->_top,$this,$id);
		}
		return $this->_cashe_obj[$id];
	}
	
	public function length() {
		return count($this->_banner_list); 
	}
	
	/**
	 * Returns pointer to instance of SingleBanner class  
	 *
	 * @param Integer $idx
	 * @return SingleBanner
	 */
	public function item($idx) {
		if (!isset($this->_banner_list[$idx])) {
			throw $this->_top->Utils->PushError('SingleBanner object does not exsists in the collection of SingleBannerList','Системная ошибка. Обратитесь к разработчикам');
		}
		
		return $this->getById($this->_banner_list[$idx]);
		
	}
	
	
	
	public function add() {
		$cfg = $this->config;
		$fields = '';
		$values = '';
		for ($c=0;$c<count($cfg);$c++) {
			$fields .= ",`{$cfg[$c][1]}`";
			$values .= ",'{$this->_top->Content($cfg[$c][2])->add()->_content_id}'";
		}
		
		$cover_id = $this->_top->Cover->add()->_content_id;
		
		$this->_top->_sql->exeDummy("INSERT INTO {$this->_top->_sql->tab_pref}banner(add_date,banner_list_id,cover_id$fields) VALUES(NOW(),{$this->_content_id},{$cover_id}{$values});");
		$id = $this->_top->_sql->last_insert_id();
		
		$this->loader();
		
		return $this->getById($id);
	}
	
	
	public function deleter() {
		$banner_length = $this->length();
		for ($l=0;$l<$banner_length;$l++) {
			$this->getById($l)->delete();
		}
		$this->_top->_sql->exeDummy("DELETE FROM {$this->_top->_sql->tab_pref}banner_list WHERE banner_list_id={$this->_content_id}");
	}
	
}

class SingleBanner extends A_SingleContent {
	
	/**
	 * Returns poiner to parent SingleNews object
	 *
	 * @var SingleBannerList
	 */
	public $list;
	/**
	 * Returns banner image
	 *
	 * @var SingleCover
	 */
	public $image;
	private $_extra_data_map;
	private $_extra_data_fields = array();
	
	protected function loader()
	{
		unset($this->list);
		unset($this->image);
		
		$get_data = $this->_top->_sql->exe("SELECT * FROM {$this->_top->_sql->tab_pref}banner c WHERE banner_id={$this->_content_id}");
		if (count($get_data)==0)
			throw $this->_top->Utils->PushError("SingleBanner object Id: {$this->_content_id} not found", 'Системная ошибка. Обратитесь к разработчикам');
		else {
			$this->_data = $get_data[0];
		}
		
		$cfg = $this->list->config;
		for ($c=0;$c<count($cfg);$c++) {
			$this->_extra_data_map[$cfg[$c][0]] = $cfg[$c];
			array_push($this->_extra_data_fields,$cfg[1]);
		}
		
		return true;
	}
	
	
	public function __get($param) {
		if (isset($this->_data[$param])) {
			return $this->_data[$param];
		} elseif ($param=='list') {
			return $this->_parent;
		} elseif ($param=='image') {
			return $this->_top->Cover->getById($this->cover_id);
		} elseif (isset($this->_extra_data_map[$param])) {
			return $this->_top->Content($this->_extra_data_map[$param][2])->getById($this->__get($this->_extra_data_map[$param][1]));
		} else {
			echo $param;
			throw $this->_top->Utils->PushError('Geting Undefinded SingleBanner object property','Системная ошибка. Обратитесь к разработчикам');
		}
		
	}
	
	public function __set($param,$value) {
		if ($param=='banner_id' || $param=='banner_list_id' || !isset($this->_data[$param]) || in_array($param,$this->_extra_data_fields)) {
			throw $this->_top->Utils->PushError('Setting Undefinded or Resticted SingleBanner object property','Системная ошибка. Обратитесь к разработчикам');
		} else {
			$this->_data[$param] = $value;
			$ins_value = $this->_top->Utils->prepareValueForSQL($value);
			$this->_top->_sql->exeDummy("UPDATE {$this->_top->_sql->tab_pref}banner SET `$param`='$ins_value' WHERE banner_id={$this->_content_id}");
		}
	}
	
	public function deleter() {
		$cfg = $this->list->config;
		for ($c=0;$c<count($cfg);$c++) {
			//echo $this->__get($cfg[$c][1]); //"Field: {$cfg[$c][1]}\r\n";
			$this->_top->Content($cfg[$c][2])->getById($this->__get($cfg[$c][1]))->delete();
		}
		$this->_top->_sql->exeDummy("DELETE FROM {$this->_top->_sql->tab_pref}banner WHERE banner_id={$this->_content_id}");
		$this->_parent->loader();
	}
	
}
?>